
import textwrap

from tests.Tester import HedyTester


class TestsLevel13(HedyTester):
    level = 13

    def test_else_with_colon(self):
        code = textwrap.dedent("""\
            if nice_trip == 'yes':
                print('Lovely!')
            else:
                print('Sorry to hear that.')
                print('Hopefully you can take a nice rest in you room.')""")
        expected = self.dedent(
            """\
            if 'nice_trip' == 'yes':
              print(f'''Lovely!''')
            else:
              print(f'''Sorry to hear that.''')
              print(f'''Hopefully you can take a nice rest in you room.''')""")

        self.multi_level_tester(code=code, expected=expected, max_level=14)

    def test_and(self):
        code = textwrap.dedent("""\
            naam is ask 'hoe heet jij?'
            leeftijd is ask 'hoe oud ben jij?'
            if naam is 'Felienne' and leeftijd is 37
                print 'hallo jij!'""")
        expected = self.dedent(
            self.input_transpiled('naam', 'hoe heet jij?'),
            self.input_transpiled('leeftijd', 'hoe oud ben jij?'),
            """\
            if naam.data == 'Felienne' and leeftijd.data == 37:
              print(f'''hallo jij!''')""")

        self.multi_level_tester(code=code, expected=expected, max_level=14)

    def test_equals(self):
        code = textwrap.dedent("""\
            name = ask 'what is your name?'
            age = ask 'what is your age?'
            if name is 'Hedy' and age is 2
                print 'You are the real Hedy!'""")

        expected = self.dedent(
            self.input_transpiled('name', 'what is your name?'),
            self.input_transpiled('age', 'what is your age?'),
            """\
            if name.data == 'Hedy' and age.data == 2:
              print(f'''You are the real Hedy!''')""")
        # TODO: expected commands failing, disabling for now
        self.multi_level_tester(
            code=code,
            max_level=14,
            expected=expected,
            # expected_commands=['ask', 'ask', 'if', 'and', 'print']
        )

    def test_or(self):
        code = textwrap.dedent("""\
            if 5 is 5 or 4 is 4
                print 'hallo'""")
        expected = textwrap.dedent("""\
            if 5 == 5 or 4 == 4:
              print(f'''hallo''')""")

        self.multi_level_tester(
            code=code,
            max_level=16,
            expected=expected,
            expected_commands=['if', 'or', 'print']
        )

    def test_simple_function(self):
        self.maxDiff = None
        code = textwrap.dedent("""\
        def simple_function_1(parameter):
            print "simple_function_1 - 1"
            m = "simple_function_1 - 2"
            print m
        def simple_function_2(param):
            print "simple_function_2 - 1"
            print param
        def simple_function_3(param_a, param_b, param_c):
            if param_a = "A" or param_a = "B"
                print "simple_function_3 - 1"
                print param_b
            else
                print "simple_function_3 - 2"
                if param_a = "B" and param_b = "test1"
                    print "simple_function_3 - 2A"
                    print param_b
                else
                    print "simple_function_3 - 2B"
                    print param_c
        a = "test1"
        simple_function_3('A', a, 1.0)
        simple_function_3('B', a, 1.0)
        simple_function_3('C', a, 1.0)
        simple_function_3('C', 3 + 3, 1.0)""")

        expected = textwrap.dedent("""\
        def simple_function_1(parameter):
          print(f'''simple_function_1 - 1''')
          m = Value('simple_function_1 - 2')
          print(f'''{m}''')
        def simple_function_2(param):
          print(f'''simple_function_2 - 1''')
          print(f'''{param}''')
        def simple_function_3(param_a, param_b, param_c):
          if param_a.data == 'A' or param_a.data == 'B':
            print(f'''simple_function_3 - 1''')
            print(f'''{param_b}''')
          else:
            print(f'''simple_function_3 - 2''')
            if param_a.data == 'B' and param_b.data == 'test1':
              print(f'''simple_function_3 - 2A''')
              print(f'''{param_b}''')
            else:
              print(f'''simple_function_3 - 2B''')
              print(f'''{param_c}''')
        a = Value('test1')
        simple_function_3(Value('A'), a, Value(1.0, num_sys='Latin'))
        simple_function_3(Value('B'), a, Value(1.0, num_sys='Latin'))
        simple_function_3(Value('C'), a, Value(1.0, num_sys='Latin'))
        simple_function_3(Value('C'), Value(3 + 3, num_sys='Latin'), Value(1.0, num_sys='Latin'))""")

        output = textwrap.dedent("""\
        simple_function_3 - 1
        test1
        simple_function_3 - 1
        test1
        simple_function_3 - 2
        simple_function_3 - 2B
        1.0
        simple_function_3 - 2
        simple_function_3 - 2B
        1.0""")

        self.multi_level_tester(
            code=code,
            expected=expected,
            output=output,
            unused_allowed=True,
            max_level=16,
            skip_faulty=False
        )

    def test_color_in_function(self):
        code = textwrap.dedent("""\
            def move(color):
                color color
            c = 'blue'
            move(c)""")

        expected = self.dedent(
            "def move(color):",
            (self.color_transpiled('{color}'), '  '),
            "c = Value('blue')",
            "move(c)"
        )

        self.multi_level_tester(
            code=code,
            expected=expected,
            max_level=16,
            extra_check_function=self.is_turtle(),
        )
